local jinghe = fk.CreateSkill {
    name = "gai__jinghe",
  }
  
  Fk:loadTranslationTable{
    ["gai__jinghe"] = "经合",
    [":gai__jinghe"] = "出牌阶段限一次，你可以展示至多四张牌名各不同的手牌，使用其中一张牌并弃置剩余牌，然后选择等量的角色，从“写满技能的天书”随机展示四个技能，这些角色"..
    "依次选择并获得其中一个，直到你下回合开始或你死亡。",
  
    ["#gai__jinghe"] = "经合：展示至多四张牌名各不同的手牌，使用其中一张并弃置剩余牌，令等量的角色获得技能",
    [ "#jinghe-ask"] = "经合：选择一张牌使用",
    ["#jinghe-choice"] = "经合：选择你要获得的技能",
    ["#jinghe-use"] = "经合：请使用%arg",
  
    ["$gai__jinghe1"] = "大哉乾元，万物资始。",
    ["$gai__jinghe2"] = "无极之外，复无无极。",
  }
  
  jinghe:addEffect("active", {
    anim_type = "support",
    prompt = "#gai__jinghe",
    min_card_num = 1,
    min_target_num = 1,
    can_use = function(self, player)
      return player:usedSkillTimes(jinghe.name, Player.HistoryPhase) == 0
    end,
    card_filter = function(self, player, to_select, selected)
      return #selected < 4 and table.contains(player:getCardIds("h"), to_select) and
        not table.find(selected, function(id)
          return Fk:getCardById(to_select).trueName == Fk:getCardById(id).trueName
        end)
    end,
    target_filter = function(self, player, to_select, selected, selected_cards)
      return #selected < #selected_cards
    end,
    feasible = function (self, player, selected, selected_cards)
      return #selected > 0 and #selected == #selected_cards
    end,
    on_use = function(self, room, effect)
      local player = effect.from
      room:sortByAction(effect.tos)
      
      -- 先展示手牌
      player:showCards(effect.cards)
      
      -- 让玩家选择其中一张牌使用
      local cards = table.filter(effect.cards, function (id)
        local card = Fk:getCardById(id)
        return player:canUse(card)
      end)
      
      if #cards == 0 then
        -- 如果没有可以使用的牌，则弃置所有牌
        room:throwCard(effect.cards, jinghe.name, player, player)
      else
        local card
        if #cards == 1 then
          card = Fk:getCardById(cards[1])
          -- 直接询问使用，可以选择所有合法目标
          room:askToUseRealCard(player, {
            pattern = {card.id},
            skill_name = jinghe.name,
            prompt = "#jinghe-use:::"..card:toLogString(),
            cancelable = false,
          })
        else
          local selected_card = room:askToCards(player, {
            min_num = 1,
            max_num = 1,
            include_equip = false,
            skill_name = jinghe.name,
            pattern = tostring(Exppattern{ id = cards }),
            prompt = "#jinghe-ask",
            cancelable = false,
          })
          card = Fk:getCardById(selected_card[1])
          -- 直接询问使用，可以选择所有合法目标
          room:askToUseRealCard(player, {
            pattern = {card.id},
            skill_name = jinghe.name,
            prompt = "#jinghe-use:::"..card:toLogString(),
            cancelable = false,
          })
        end
        
        -- 弃置剩余牌
        local to_discard = table.filter(effect.cards, function(id)
          return id ~= card.id
        end)
        if #to_discard > 0 then
          room:throwCard(to_discard, jinghe.name, player, player)
        end
      end
      
      -- 继续分配技能
      local mark = player:getTableMark("jinghe_data")
      local skills = table.random(
        {"ex__leiji", "yinbingn", "huoqi", "guizhu", "xianshou", "lundao", "guanyue", "yanzhengn",
          "ex__biyue", "ex__tuxi", "ty_ex__mingce", "ty_ex__zhiyan"
        }, 4)
      local selected = {}
      for _, p in ipairs(effect.tos) do
        if not p.dead then
          local choices = table.filter(skills, function(s)
            return Fk.skills[s] and not p:hasSkill(s, true) and not table.contains(selected, s)
          end)
          if #choices > 0 then
            local choice = room:askToChoice(p, {
              choices = choices,
              skill_name = jinghe.name,
              prompt = "#jinghe-choice",
              detailed = true,
              all_choices = skills,
            })
            table.insert(selected, choice)
            room:handleAddLoseSkills(p, choice)
            table.insert(mark, {p.id, choice})
            room:addTableMark(player, "jinghe-turn", p.id)
          end
        end
      end
      room:setPlayerMark(player, "jinghe_data", mark)
    end,
  })
  
  local spec = {
    can_refresh = function(self, event, target, player, data)
      return target == player and player:getMark("jinghe_data") ~= 0
    end,
    on_refresh = function(self, event, target, player, data)
      local room = player.room
      local mark = player:getMark("jinghe_data")
      room:setPlayerMark(player, "jinghe_data", 0)
      for _, dat in ipairs(mark) do
        local p = room:getPlayerById(dat[1])
        room:handleAddLoseSkills(p, "-"..dat[2])
      end
    end,
  }
  
  jinghe:addEffect(fk.TurnStart, spec)
  jinghe:addEffect(fk.Death, spec)
  
  return jinghe